import { isArrayLike, getLength } from './each'
import values from '../object/values'
import random from '../util/random'
import clone from '../object/clone'
export default function sample(obj: number[], n?, guard?) {
  if (n == null || guard) {
    if (!isArrayLike(obj)) obj = values(obj)
    return obj[random(obj.length)]
  }
  const sample: number[] = isArrayLike(obj) ? clone(obj) : values(obj)
  var length: number = getLength(sample);
  n = Math.max(Math.min(n, length), 0)
  var last = length - 1
  for (var index = 0; index < n; index++) {
    var rand = random(index, last)
    var temp = sample[index]
    sample[index] = sample[rand]
    sample[rand] = temp
  }
  return sample.slice(0, n)
}